﻿Public Class frmComCode
    Dim sql As String
    Dim dsComCode As DataSet
    Dim daComCode As OleDb.OleDbDataAdapter
    Dim cmbGroupSearchAction As Boolean

    'Private Sub frmComCode_SizeChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.SizeChanged
    '    If Me.WindowState = FormWindowState.Maximized Then
    '        'MsgBox("The form has been Maximized.")
    '    Else
    '        'grdComCode.Size.Width = 587
    '        'grdComCode.Size.Height = 513
    '    End If
    'End Sub
    Private Sub frmComCode_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        e.Cancel = True
        Me.Hide()
    End Sub

    Private Sub frmComCode_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call gSubIconShow(Me)
        Call mkCmbGroupCd(cmbGroupCd)
        Call mkCmbGroupCd(cmbSearchGroup)
        Call gridRefresh("default")
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Call gridRefresh("")

    End Sub

    Private Sub btnSearchReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearchReset.Click
        txtSearch.Clear()
        cmbSearchGroup.SelectedValue = 0
        Call gridRefresh("")
    End Sub

    Private Sub txtSearch_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSearch.KeyPress
        If Asc(e.KeyChar) = 13 Then
            Call gridRefresh("")
        End If
    End Sub

    Private Sub cmbSearchGroup_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles cmbSearchGroup.KeyPress
        e.Handled = True
    End Sub

    Private Sub cmbSearchGroup_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbSearchGroup.SelectedIndexChanged
        If cmbSearchGroup.SelectedValue.ToString <> "0" And cmbGroupSearchAction = True Then
            Call gridRefresh("")
        End If
    End Sub

    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        Call txtReset()
    End Sub

    Private Sub cmbFlag_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles cmbFlag.KeyPress
        e.Handled = True
    End Sub

    Private Sub cmbGroupCd_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles cmbGroupCd.KeyPress
        e.Handled = True
    End Sub

#Region "프로시져"

    Private Sub gridRefresh(initFlag)
        Call dbConnection()
        Dim addSql As String = ""

        dsComCode = New DataSet
        If txtSearch.Text <> "" Then
            addSql = " AND itemCd LIKE '%" + txtSearch.Text + "%' "
        End If
        If cmbSearchGroup.SelectedValue.ToString <> "0" Then
            addSql = " AND groupCd='" + cmbSearchGroup.SelectedValue.ToString + "' "
        End If
        If initFlag = "default" Then
            addSql = " AND groupCd='0'"
        End If
        sql = "SELECT groupCd as 그룹코드, groupNm as 그룹명, itemCd as 아이템코드, itemNm as 아이템, itemSeq as 표시순위, flag as 사용여부 " & _
            "   FROM tblComCode WHERE flag<>''" + addSql + " ORDER BY groupNm, itemSeq"
        'MsgBox(sql)

        Try
            daComCode = New OleDb.OleDbDataAdapter(sql, dbConn)
            daComCode.Fill(dsComCode, "PRODUCT")

            grdComCode.DataSource = dsComCode.Tables("PRODUCT")

            dbConn.Close()
            Call setGridDesign()

        Catch err As ObjectDisposedException
            dbConn.Close()
            MsgBox(sql)

            MsgBox(err.Message, MsgBoxStyle.Critical, "오류")
        End Try
    End Sub
    Private Sub setGridDesign()

        grdComCode.Columns(0).Visible = False
        grdComCode.Columns(1).Width = "120"
        grdComCode.Columns(2).Width = "90"
        grdComCode.Columns(3).Width = "120"
        grdComCode.Columns(4).Width = "80"
        grdComCode.Columns(5).Width = "130"

        With grdComCode.DefaultCellStyle
            .Alignment = DataGridViewContentAlignment.MiddleCenter

        End With
        'grdComCode.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft

        gSubMkCmbFlag(cmbFlag) '## 삭제 여부 콤보박스 초기화
        cmbFlag.SelectedIndex = 0

    End Sub

    Private Sub mkCmbGroupCd(ByVal obj As Object)
        dbConnection()
        cmbGroupSearchAction = False
        sql = "SELECT [groupCd], [groupNm] FROM tblComCode WHERE flag='Y' GROUP BY groupCd, groupNm ORDER BY groupNm"
        dsComCode = New DataSet
        daComCode = New OleDb.OleDbDataAdapter(sql, dbConn)
        daComCode.Fill(dsComCode, "cmbComCode")

        dbConn.Close()
        Dim newDsRow As DataRow = dsComCode.Tables("cmbComCode").NewRow
        newDsRow("groupNm") = "-코드그룹-"
        newDsRow("groupCd") = "0"

        dsComCode.Tables("cmbComCode").Rows.Add(newDsRow)
        With obj
            .DataSource = dsComCode.Tables("cmbComCode")
            .DisplayMember = "groupNm"
            .ValueMember = "groupCd"
            '.SelectedIndex = 0
        End With
        obj.SelectedIndex = dsComCode.Tables("cmbComCode").Rows.Count - 1
        cmbGroupSearchAction = True
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Call dbConnection()
        Dim oleCmd As OleDb.OleDbCommand
        Dim oleTran As OleDb.OleDbTransaction
        Dim rtnMsg As String
        Dim valGroupCd, valGroupNm, valItemCd, valItemNm, valItemSeq, valFlag As String
        Dim curDt As DateTime = Now

        If cmbGroupCd.SelectedIndex = -1 Then
            MsgBox("그룹을 선택해 주세요")
            cmbGroupCd.Focus()
            dbConn.Close()
            Exit Sub
        End If

        valGroupCd = cmbGroupCd.SelectedValue
        valGroupNm = cmbGroupCd.Text
        valItemCd = gFnChkVal(txtItemCd.Text)
        valItemNm = gFnChkVal(txtItemNm.Text)
        valItemSeq = gFnChkVal(txtItemSeq.Text)
        valFlag = cmbFlag.SelectedValue

        If valItemCd = "" Then
            MsgBox("아이템 코드를 입력해 주세요")
            txtItemCd.Focus()
            dbConn.Close()
            Exit Sub
        End If
        If valItemNm = "" Then
            MsgBox("아이템 이름을 입력해 주세요")
            txtItemNm.Focus()
            dbConn.Close()
            Exit Sub
        End If

        If txtMode.Text = "" Then
            sql = "INSERT INTO tblComCode(groupCd, groupNm, itemNm, itemCd, itemSeq,  flag, addDt) VALUES('" + valGroupCd + "','" + valGroupNm + "','" + valItemNm + "','" + valItemCd + "','" + valItemSeq + "','" + valFlag + "','" + curDt + "')"
            rtnMsg = "공통 코드가 등록되었습니다"
        Else
            sql = "UPDATE tblComCode SET groupNm='" + valGroupNm + "', itemNm='" + valItemNm + "', itemSeq='" + valItemSeq + "', flag='" + valFlag + "', updateDt='" + curDt + "' WHERE groupCd='" + cmbGroupCd.SelectedValue.ToString + "' AND itemCd='" + txtItemCd.Text + "'"
            rtnMsg = "공통 코드가 수정되었습니다"
        End If

        oleCmd = New OleDb.OleDbCommand(sql, dbConn)

        oleTran = dbConn.BeginTransaction
        oleCmd.Transaction = oleTran
        Try

            oleCmd.ExecuteNonQuery()
            oleTran.Commit()

            dbConn.Close()
            MsgBox(rtnMsg)
            Call gridRefresh("")
            Call txtReset()
        Catch err As OleDb.OleDbException
            oleTran.Rollback()
            dbConn.Close()
            MsgBox(sql)

            MsgBox(err.Message, MsgBoxStyle.Critical, "오류")
        End Try
    End Sub

    Private Sub grdComCode_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdComCode.CellClick
        If e.RowIndex = -1 Then
            Exit Sub
        End If
        If IsDBNull(grdComCode.Rows(e.RowIndex).Cells(0).Value) = True Then
            Exit Sub
        End If
        sql = "SELECT groupCd, itemCd, itemNm, itemSeq, flag  FROM tblComCode WHERE groupCd='" & grdComCode.Rows(e.RowIndex).Cells(0).Value & "' AND itemCd='" & grdComCode.Rows(e.RowIndex).Cells(2).Value & "'"

        Call dbConnection()
        dsComCode = New DataSet
        daComCode = New OleDb.OleDbDataAdapter(sql, dbConn)
        daComCode.Fill(dsComCode, "comCodeInfo")

        dbConn.Close()

        txtReset()

        txtMode.Text = "upate"
        cmbGroupCd.SelectedValue = dsComCode.Tables("comCodeInfo").Rows(0).Item(0)
        txtItemCd.Text = dsComCode.Tables("comCodeInfo").Rows(0).Item(1)

        If IsDBNull(dsComCode.Tables("comCodeInfo").Rows(0).Item(2)) = False Then
            txtItemNm.Text = dsComCode.Tables("comCodeInfo").Rows(0).Item(2)
        End If
        If IsDBNull(dsComCode.Tables("comCodeInfo").Rows(0).Item(3)) = False Then
            txtItemSeq.Text = dsComCode.Tables("comCodeInfo").Rows(0).Item(3)
        End If
        cmbFlag.SelectedValue = dsComCode.Tables("comCodeInfo").Rows(0).Item(4)
    End Sub

    Private Sub txtReset()
        cmbGroupCd.SelectedValue = 0
        txtItemCd.Clear()
        txtItemNm.Clear()
        txtItemSeq.Clear()
        txtMode.Clear()
        cmbFlag.SelectedIndex = 0
    End Sub
#End Region


End Class